use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, Result, AsyncWriteExt};
use std::path::Path;

/// 阅读文件
pub async fn read_file(file_name: &str) -> Result<Vec<u8>> {
    let mut file = File::open(file_name).await?;
    let mut contents = vec![];
    file.read_to_end(&mut contents).await?;
    Ok(contents)
}

/// 文件是否存在
pub async fn is_file(file_name: &str) -> bool {
    Path::new(file_name).exists()
}

/// 创建文件
pub async fn create_file(file_name: &str) -> Result<File> {
    let file = File::create(file_name).await?;
    Ok(file)
}

/// 创建文件
pub async fn create_file_with_content(file_name: &str, content: String) -> Result<File> {
    let mut file = File::create(file_name).await?;
    file.write_all(content.as_bytes()).await?;
    Ok(file)
}

/// 文件修改
pub async fn modify_file_by_name(file_name: &str, append: bool, content: String) -> Result<()> {
    let mut file = OpenOptions::new().append(append).open(file_name).await?;
    file.write_all(content.as_bytes()).await?;
    Ok(())
}

/// 文件删除
pub async fn delete_file(file_name: &str) -> Result<()> {
    tokio::fs::remove_file(Path::new(file_name)).await?;
    Ok(())
}
